from flask import request, session, jsonify

from models import db
from models.index import Collection, Comment, CommentLike
from . import news_blu


@news_blu.route("/collect", methods=["POST"])
def collect():
    news_id = request.json.get("news_id")
    # do表示关注，undo表示取消关注
    action = request.json.get("action")

    # 获取当前用户的id
    user_id = session.get("user_id")
    if not user_id:
        ret = {
            "errno": "5003",
            "errmsg": "请先登录"
        }
        return ret
    if action == 'do':
        collection = Collection()
        collection.user_id = user_id
        collection.news_id = news_id
        try:
            db.session.add(collection)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "收藏成功"
            }
        except Exception as ret:
            ret = {
                "errno": "5001",
                "errmsg": "收藏失败"
            }
    elif action == 'undo':
        collection = db.session.query(Collection).filter(Collection.news_id == news_id,
                                                         Collection.user_id == user_id).first()
        if collection:
            db.session.delete(collection)
            db.session.commit()
            ret = {
                "errno": "0",
                "errmsg": "取消收藏成功"
            }
        else:
            ret = {
                "errno": "5002",
                "errmsg": "取消收藏失败"
            }
    return jsonify(ret)


@news_blu.route("/comment", methods=["POST"])
def news_comment():
    # 1. 提取用户评价的数据
    content = request.json.get("content")
    news_id = request.json.get("news_id")
    user_id = session.get("user_id")
    parent_id = request.json.get("parent_id")

    # 2. 保存到数据库
    new_comment = Comment()
    new_comment.news_id = news_id
    new_comment.user_id = user_id
    new_comment.content = content
    new_comment.parent_id = parent_id
    db.session.add(new_comment)
    db.session.commit()
    if not new_comment.parent_id:
        ret = {
            "errno": 0,
            "errmsg": "成功"
        }
        return jsonify(ret)

    user = new_comment.user.__dict__
    parent = new_comment.parent.__dict__
    comment = new_comment.__dict__
    parent_user = new_comment.parent.user.__dict__
    del user['_sa_instance_state']
    del parent['_sa_instance_state']
    del comment['_sa_instance_state']
    parent['user'] = parent_user

    comment['user'] = user
    comment['parent'] = parent
    print(comment)
    ret = {
        "errno": 0,
        "errmsg": "成功",
        "comment": comment
    }
    return jsonify(ret)


@news_blu.route("/comment_like", methods=["POST"])
def comment_like():
    action = request.json.get("action")
    comment_id = request.json.get("comment_id")
    user_id = session.get("user_id")
    if not user_id:
        ret = {
            "errno": 4101,
            "errmsg": "用户未登录"
        }
        return jsonify(ret)
    if action == "do":
        commentLike = CommentLike(user_id=user_id, comment_id=comment_id)
        try:
            db.session.add(commentLike)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "点赞成功"
            }
            return jsonify(ret)
        except:
            ret = {
                "errno": 4102,
                "errmsg": "操作失败"
            }
            return jsonify(ret)
    else:
        commentLike = db.session.query(CommentLike).filter(CommentLike.user_id == user_id,
                                                           CommentLike.comment_id == comment_id).first()
        try:
            db.session.delete(commentLike)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "取消点赞成功"
            }
            return jsonify(ret)
        except:
            ret = {
                "errno": 4102,
                "errmsg": "操作失败"
            }
            return jsonify(ret)
